package com.terabits.smartriver.util;

import com.terabits.smartriver.constants.DatagramConst;

/**
 * Created by Yin on 2019/5/14 0014
 */
public class DataValida {
    /**
     * 校验数据帧头帧尾是否符合协议规范
     *
     * @param data 通信原始数据
     * @return
     */
    public static boolean ValidateHeadAndFeet(String data) {
        boolean state = false;
        if (DatagramConst.HEAD0.equals(data.substring(0, 4)) || DatagramConst.HEAD1.equals(data.substring(0, 5)) || DatagramConst.HEAD2.equals(data.substring(0, 6)) || DatagramConst.HEAD3.equals(data.substring(0, 7)) || DatagramConst.HEAD4.equals(data.substring(0, 8)) || DatagramConst.HEAD5.equals(data.substring(0, 9))) {
            return true;
        }
        return state;
    }

    public static void main(String[] args) {
        String data = "#STA:5720001202002211;L:0145;TM:200221160339;C:06;A01:19.71;A02:00000;A03:00000;A04:00000;A05:7.060;A06:790.7;A07:10.10;A08:1258.;K01:00000000000;78;#";
        int headeIndex = data.indexOf("STA");
        String head = data.substring(0, headeIndex);
        int type = head.length();

        System.out.println(type);
    }

    /**
     * 返回数据包类型
     *
     * @param data
     * @return 1绿维1 2绿维2 3普通数据包 4PLC01数据包 5PLC03数据包
     */
    public static int getPackageType(String data) {
        int headeIndex = data.indexOf("STA");
        String head = data.substring(0, headeIndex);
        int type = head.length();
        if (type == 3 || type == 4 || type == 5) {
            return type;
        }
        if (type == 0) {
            return 0;
        }
        if (-1 != data.indexOf("P01")) {
            return 1;
        }

        if (-1 != data.indexOf("P13")) {
            return 2;
        }
        return -1;


    }

    /**
     * CRC16校验
     *
     * @param data    内容数据
     * @param crcCode 附带CRC校验码
     * @return
     */
//    public static boolean ValidateCRCCode(String data, String crcCode) {
//        boolean state = false;
//        if (crcCode.equals(CRC16MySelf.getCRC(data.getBytes()))) {
//            return true;
//        }
//        return state;
//    }
    public static boolean ValidateSUMCode(String data, String sumCode) {
        boolean state = true;
//        if (sumCode.equals(CRC16MySelf.getCRC(data.getBytes()))) {
//            return true;
//        }
        return state;
    }
}
